package com.example.exoplayerdemo.ui;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.example.exoplayerdemo.R;
import com.google.android.exoplayer2.source.ads.AdsLoader;

import java.util.List;

/**
 * 横竖屏切换
 *
 * @author caowj
 * https://www.jianshu.com/p/6f9bf41ddc35
 * https://blog.csdn.net/u010072711/article/details/51517170
 * <p>
 * https://blog.csdn.net/greatyoulv/article/details/104023467
 * @deprecated see {@link MyExoPlayerView}
 **/
public class ExoPlayerViewEx extends MyExoPlayerView {
    /**
     * 切换到全屏播放前当前VideoPlayerView的宽度
     */
    private int mVideoWidth;

    /**
     * 切换到全屏播放前当前VideoPlayerView的高度
     */
    private int mVideoHeight;

    /**
     * 切换全屏播放前当前VideoPlayerView的在父容器中的索引
     */
    private int mOldIndex = 0;


    public ExoPlayerViewEx(Context context) {
        super(context);
        init();
    }

    public ExoPlayerViewEx(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ExoPlayerViewEx(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();

    }

    public static Activity getActivity(Context context) {
        if (context == null) {
            return null;
        }

        if (context instanceof Activity) {
            return (Activity) context;
        } else if (context instanceof ContextWrapper) {
            return getActivity(((ContextWrapper) context).getBaseContext());
        }

        return null;
    }

    private void init() {
        Log.d("caowj", "init....");
        handleFullVideo();
    }

    private ExoPlayerViewEx getSelf() {
        return this;
    }

    private void handleFullVideo() {
        MyPlayerControlView controlView = null;

        //旧版本写法：
//        View[] views = getAdOverlayViews();
//        for (View view : views) {
//            if (view instanceof MyPlayerControlView2) {
//                controlView2 = (MyPlayerControlView2) view;
//                break;
//            }
//        }

        List<AdsLoader.OverlayInfo> views = getAdOverlayInfos();
        for (AdsLoader.OverlayInfo info : views) {
            if (info.view instanceof MyPlayerControlView) {
                controlView = (MyPlayerControlView) info.view;
                break;
            }
        }

        if (controlView != null) {
            View fullControllerView = controlView.findViewById(R.id.exo_fullscreen);
            fullControllerView.setOnClickListener(v -> {
                int direction = getActivity(getContext()).getResources().getConfiguration().orientation;
                if (direction == Configuration.ORIENTATION_PORTRAIT) {
                    startFullScreen();
                } else if (direction == Configuration.ORIENTATION_LANDSCAPE) {
                    exitFullScreen();
                } else {
                    Log.d("caowj", "未处理的direction：" + direction);
                }
            });
        } else {
            Log.w("caowj", "controlView is null");
        }
    }

    private void startFullScreen() {
        // 设置为横屏模式
        getActivity(getContext()).getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getActivity(getContext()).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        //防止屏幕旋转，数据重置。
        if (mVideoWidth <= 0) {
            mVideoWidth = this.getWidth();
        }
        if (mVideoHeight <= 0) {
            mVideoHeight = this.getHeight();
        }

        Log.d("caowj", "1111mVideoWidth:" + mVideoWidth + ",mVideoHeight:" + mVideoHeight);
        // 切换全屏播放前当前VideoPlayerView的父容器
        ViewGroup viewGroup = (ViewGroup) this.getParent();
        mOldIndex = viewGroup.indexOfChild(this);
        viewGroup.removeAllViews();

        LayoutParams lp = new LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        lp.setMargins(0, 0, 0, 0);
        viewGroup.addView(getSelf(), lp);
    }

    private void exitFullScreen() {
        // 设置为竖屏模式
        getActivity(getContext()).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        getActivity(getContext()).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // 切换全屏播放前当前VideoPlayerView的父容器
        ViewGroup viewGroup = (ViewGroup) this.getParent();
        mOldIndex = viewGroup.indexOfChild(this);

        Log.d("caowj", "222mVideoWidth:" + mVideoWidth + ",mVideoHeight:" + mVideoHeight);
        LayoutParams lp = new LayoutParams(mVideoWidth, mVideoHeight);
        viewGroup.removeView(getSelf());
        viewGroup.addView(getSelf(), mOldIndex, lp);

        viewGroup = null;
        mOldIndex = 0;
    }

}

